/**
 * Initial version of the Gannt chart view
 * This version only prints text to the console
 *
 * To do:
 * -verify output values are correct, the print function is pretty lazy
 * -implement the graphical component
 * 
 * Notable
 * -only public methods are constructor and chartUpdate(Object[]) which is currently called by the UI
 * -implementation in the UI is lazy atm, generating a new ChartView with each call, in the future will be corrected
 *
 *
 * @author Jon Leckman
 * @version 1.00 2012/3/26
 */

package SOEN_Phase_2;

// *** Import SWING GUI libraries *** //
// General window (for use when this becomes a popup)
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JTextField;

// Event handling (for use when this becomes a popup)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;




public class ChartView {

private Task[] tasks;
private int totalTime;


	//only one constructor, takes the array of objects provided by TaskTable
    public ChartView(Object[] x){
   	
   		//initializors
    	totalTime = 0;	
    	tasks = new Task[0];
    	
    	//calls method used to update the display
    	chartUpdate(x);
    }
    
    
    
    
    //chartUpdate is the only public method, it is used to set up the Task array and call the display function
    public void chartUpdate(Object[] x){
    	
    	tasks = new Task[x.length];
		
		//I'm sure there is a more elegant solution
    	for (int i=0; i<x.length ;i++)
    		tasks[i] = new Task( ((Task)x[i]).getID(), ((Task)x[i]).getDuration(), ((Task)x[i]).getTitle(), ((Task)x[i]).getAssignee(), ((Task)x[i]).getAssigneeID(), ((Task)x[i]).getTimeInvested(), ((Task)x[i]).getCompletion(), ((Task)x[i]).getSubtasks() );
    	

    	buildDisplay();
    }


	//for now display pipes value to the console, later version will be graphical
	private void buildDisplay() {
	totalTime = 0;
	    	
		for (Task task: tasks){
			
			//start each diplay element by tree tops
			if (task.getParent() == null){
				
				//get the total time and display it, this will be a header line for the chart
				totalTime = getTaskTimeTotal(task);
				
				//next we need to get each sub-task and display it with a %age of time
				displayTask(task);
						
			}	
		}
	}
	
	//called by the resursive displaySubTask() method to update the diplay frame
	//for now simply pipes to the console, but this is the method which will draw the charts
	private void updateDisplay(Task t){
		if (t.getParent() == null){
		System.out.println("MASTER TASK =" + t.getTitle() + " TASK TIME % = 100 + MASTER TASK TIME =" + totalTime);	
		}
		else{
		System.out.println("SUB TASK ="+t.getTitle() +" TASK TIME % =" + (t.getDuration()/totalTime) * 100 + " TASK TIME =" +t.getDuration());
		}
	}
	



	//this is a double recursive loop which iterates through the task tree and displays the associated data
	private void displayTask(Task t){
		if (t.getSubtasks() == null){
			updateDisplay(t);
		}
		else{
			displaySubTask(t);
		}
	}

	private void displaySubTask(Task t){
		updateDisplay(t);
		for (Task subTask : t.getSubtasks()){
			displayTask(subTask);
		}
	}




	//These two functions are a double-recursive loop needed to iterate through subtask lists to get time totals
	private int getTaskTimeTotal(Task t){
		if (t.getSubtasks() == null){
			return t.getDuration();
		}
		else return t.getDuration() + getSubTaskTimeTotal(t);
	}

	private int getSubTaskTimeTotal(Task t){
		int x = t.getDuration();
		for (Task subTask : t.getSubtasks() ){
			x += getTaskTimeTotal(subTask);
		}
		return x;
	}




    
    
    
    /*
    // Event handling routine
    @Override
    public void actionPerformed(ActionEvent event)
    {
        // Make this window visible
        this.buildDisplay();
    }
    */
}